{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 1000 cost = 11.133365\n",
      "Epoch: 2000 cost = 6.238967\n",
      "Epoch: 3000 cost = 3.833404\n",
      "Epoch: 4000 cost = 3.308231\n",
      "Epoch: 5000 cost = 3.080212\n",
      "Epoch: 6000 cost = 3.072460\n",
      "Epoch: 7000 cost = 2.962381\n",
      "Epoch: 8000 cost = 2.576309\n",
      "Epoch: 9000 cost = 2.763438\n",
      "Epoch: 10000 cost = 2.869924\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXsAAAD6CAYAAABApefCAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAHktJREFUeJzt3X9UVXX+7/HnR0RhNNFKA80f+eOr\npfljhlSE+w381Q9/fs1pzKzUq3ZX5phzbc3YXFrM3GXcpuaqgzO16M7ETJM2zRfTsPpqacyMKH5F\nIkoT5yuZiWgkgoqIiJ/7B4iBoHDknH04+/VYi+Xmw+ec8947fPXxsz97b2OtRUREAlsbpwsQERHv\nU9iLiLiAwl5ExAUU9iIiLqCwFxFxAYW9iIgLKOxFRFxAYS8i4gIKexERF2jrdAGX3XrrrbZPnz5O\nlyEi0qrs3bv3W2tt1+v185uw79OnD1lZWU6XISLSqhhjvmpKP03jiEirFxsbW+f7JUuW1G6npKSQ\nkpLi24L8kMJeRAJOUlKS0yX4HYW9iASc+iN9gH379jF27FjOnDnDuXPnmDlzJtHR0Sxev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lgTc32BScLcZK1ttxaOxQ46nQtDmvO3XYD2o38Trj2n8dOa+COon+31j5njNkNFFpr8x0qzeeu\ncSz+DWgHbHGmMt9q7Dg4VY8fuYWm3W1XrkFh75AG7ih6izGmPTAG2G6MibPWNnajuYBS/1gAGGOm\nAkuBKdbaKt9X5XsNHQcBqm8L0JS77co1aBrHf/xP4Ic1wXYOCHW4HscYY8KBZ4FJ1tozTtcjjmvq\n3XblGhT2/uO3wHxjzC7gJC6ZumjEE0AEsKXmGcbznS5IHHXV3XYdrqdV0kVVIiIuoJG9iIgLKOxF\nRFxAYS8i4gIKexERF1DYi4i4gMJeRMQFFPYiIi7w/wE8w9PNAW249QAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "tf.reset_default_graph()\n",
    "\n",
    "# 3 Words Sentence\n",
    "sentences = [ \"i like dog\", \"i like cat\", \"i like animal\",\n",
    "              \"dog cat animal\", \"apple cat dog like\", \"dog fish milk like\",\n",
    "              \"dog cat eyes like\", \"i like apple\", \"apple i hate\",\n",
    "              \"apple i movie book music like\", \"cat dog hate\", \"cat dog like\"]\n",
    "\n",
    "all_words = ' '.join(sentences).split()\n",
    "word_list = list(set(all_words))\n",
    "word_num = {w:i for i,w in enumerate(word_list)}\n",
    "\n",
    "#parameters\n",
    "embedding_size = 2\n",
    "batch_size = 20\n",
    "num_sampled = 10 # for negative sampling, less than batch_size\n",
    "vocab_size = len(word_num)\n",
    "\n",
    "def get_random_batch(data,batch_size):\n",
    "    random_target = []\n",
    "    random_labels = []\n",
    "    index = np.random.choice(len(data),batch_size,replace=False)\n",
    "    \n",
    "    for i in index:\n",
    "        random_target.append(data[i][0])\n",
    "        random_labels.append([data[i][1]])\n",
    "    return random_target,random_labels\n",
    "\n",
    "skip_gram = []\n",
    "\n",
    "for i in range(1,len(all_words)-1):\n",
    "    target = word_num[all_words[i]]#中心词\n",
    "    labels = [word_num[all_words[i-1]],word_num[all_words[i+1]]]#存储中心词前后的各一个词\n",
    "    \n",
    "    for w in labels:\n",
    "        skip_gram.append([target,w])\n",
    "\n",
    "inputs = tf.placeholder(tf.int32,[batch_size])\n",
    "labels = tf.placeholder(tf.int32,[batch_size,1])\n",
    "\n",
    "embeddings = tf.Variable(tf.random_normal([vocab_size,embedding_size],-1.0,1.0))\n",
    "selected_embeddings = tf.nn.embedding_lookup(embeddings,inputs)\n",
    "\n",
    "nce_wights = tf.Variable(tf.random_normal([vocab_size,embedding_size],-1.0,1.0))\n",
    "nce_bias = tf.Variable(tf.zeros([vocab_size]))\n",
    "\n",
    "cost = tf.reduce_mean(tf.nn.nce_loss(nce_wights,nce_bias,labels,selected_embeddings,num_sampled,vocab_size))\n",
    "optimizer = tf.train.AdamOptimizer(0.001).minimize(cost)\n",
    "\n",
    "# Training\n",
    "with tf.Session() as sess:\n",
    "    init = tf.global_variables_initializer()\n",
    "    sess.run(init)\n",
    "\n",
    "    for epoch in range(10000):\n",
    "        batch_inputs, batch_labels = get_random_batch(skip_gram, batch_size)\n",
    "        _, loss = sess.run([optimizer, cost], feed_dict={inputs: batch_inputs, labels: batch_labels})\n",
    "\n",
    "        if (epoch + 1) % 1000 == 0:\n",
    "            print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.6f}'.format(loss))\n",
    "\n",
    "    trained_embeddings = embeddings.eval()\n",
    "\n",
    "for i, label in enumerate(word_num):\n",
    "    x, y = trained_embeddings[i]\n",
    "    plt.scatter(x, y)\n",
    "    plt.annotate(label, xy=(x, y), xytext=(5, 2), textcoords='offset points', ha='right', va='bottom')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
